package com.tanhua.mysqldb.service;

import com.alibaba.nacos.common.utils.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tanhua.api.service.UserInfoApi;
import com.tanhua.model.dto.RecommendUserDto;
import com.tanhua.model.pojo.UserInfo;
import com.tanhua.mysqldb.mapper.UserInfoMapper;
import org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@DubboService
public class UserInfoApiImpl implements UserInfoApi {

    @Autowired
    private UserInfoMapper userInfoMapper;

    @Override
    public void save(UserInfo userInfo) {
        userInfoMapper.insert(userInfo);
    }

    @Override
    public void updateById(UserInfo userInfo) {
        userInfoMapper.updateById(userInfo);
    }

    @Override
    public UserInfo findById(Long userID) {
        return userInfoMapper.selectById(userID);
    }

    @Override
    public Page<UserInfo> findPageByUesrId(List<Long> userIds, RecommendUserDto dto) {

        LambdaQueryWrapper<UserInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        lambdaQueryWrapper.in(CollectionUtils.isNotEmpty(userIds), UserInfo::getId, userIds);

        // 筛选条件
        if (dto != null) {
            lambdaQueryWrapper.le(dto.getAge() != null, UserInfo::getAge, dto.getAge());
            lambdaQueryWrapper.like(StringUtils.isNotBlank(dto.getCity()), UserInfo::getCity, dto.getCity());
            lambdaQueryWrapper.eq(StringUtils.isNotBlank(dto.getEducation()), UserInfo::getEducation, dto.getEducation());
            lambdaQueryWrapper.eq(StringUtils.isNotBlank(dto.getGender()), UserInfo::getGender, dto.getGender());
//            lambdaQueryWrapper.eq(StringUtils.isNotBlank(dto.getLastLogin()), UserInfo::get)
        }

        Page<UserInfo> pageInfo = new Page<>(dto.getPage(),dto.getPagesize());
        userInfoMapper.selectPage(pageInfo, lambdaQueryWrapper);

        return pageInfo;
    }

    @Override
    public List<UserInfo> findByIds(List<Long> ids) {
        return userInfoMapper.selectList(
                Wrappers.lambdaQuery(UserInfo.class)
                        .in(UserInfo::getId,ids)
        );
    }

    @Override
    public Page<UserInfo> findByIdsLikeName(List<Long> userIds, String keyword, Integer page, Integer pagesize) {

        return userInfoMapper.selectPage(new Page<>(page, pagesize),
                Wrappers.lambdaQuery(UserInfo.class)
                        .in(UserInfo::getId, userIds)
                        .like(StringUtils.isNotBlank(keyword), UserInfo::getNickname, keyword));
    }

    @Override
    public Page<UserInfo> findByPage(Integer page, Integer pagesize, Long id, String nickname, String city) {

        Page<UserInfo> pageInfo = new Page<>(page, pagesize);

        LambdaQueryWrapper<UserInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(id != null, UserInfo::getId,id)
                .like(StringUtils.isNotBlank(nickname), UserInfo::getNickname, nickname)
                .like(StringUtils.isNotBlank(city), UserInfo::getCity, city);

        return userInfoMapper.selectPage(pageInfo, lambdaQueryWrapper);
    }
}
